package org.itboys.core.controller.front.small;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.core.entity.mongo.SundayAddress;
import org.itboys.core.service.mongo.front.SundayFrontAddressService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.spring.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2018/10/15
 * 描述：地址
 */
@Controller
@RequestMapping("/sunday/small/address")
@Api(value = "address", description = "收货地址")
public class SundaySmallAddressController extends BaseController {
    @Autowired
    private SundayFrontAddressService addressService;


    @RequestMapping(value = "/select", method = {RequestMethod.POST})
    @ApiOperation(value = "查找当前用户所有收货地址。不分页")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "用户Id") @RequestParam(value = "memberId", required = true) Long memberId
    ) {
        List<SundayAddress> result = addressService.selectAddress(memberId);
        ResponseMessage.success(result);
    }

    /**
     * 保存或者修改收货地址
     *
     * @param request
     * @param response
     * @param id
     * @param receiveName
     * @param receiveMobile
     * @param receiveAddress
     */
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    @ApiOperation(value = "保存新增或更新用户地址")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ApiParam(value = "地址id。新增时传入0") @RequestParam(value = "id", required = false) Long id,
                     @ApiParam(value = "用户Id") @RequestParam(value = "memberId", required = true) Long memberId,
                     @ApiParam(value = "收货人姓名") @RequestParam(value = "receiveName", required = true) String receiveName,
                     @ApiParam(value = "手机号码") @RequestParam(value = "receiveMobile", required = true) String receiveMobile,
                     @ApiParam(value = "省id") @RequestParam(value = "provinceId", required = true) Long provinceId,
                     @ApiParam(value = "省名称") @RequestParam(value = "provinceName", required = true) String provinceName,
                     @ApiParam(value = "市id") @RequestParam(value = "cityId", required = true) Long cityId,
                     @ApiParam(value = "市名称") @RequestParam(value = "cityName", required = true) String cityName,
                     @ApiParam(value = "区县id") @RequestParam(value = "districtId", required = true) Long districtId,
                     @ApiParam(value = "区县名称") @RequestParam(value = "districtName", required = true) String districtName,
                     @ApiParam(value = "详细地址") @RequestParam(value = "receiveAddress", required = true) String receiveAddress,
                     @ApiParam(value = "是否默认") @RequestParam(value = "isDefault", required = true) Integer isDefault
    ) {
        SundayAddress address = addressService.saveAddress(id, memberId, receiveName, receiveMobile,
                provinceId,provinceName,cityId, cityName,districtId, districtName, receiveAddress, isDefault);
        ResponseMessage.success(address);
    }

    /**
     * 设置默认地址
     *
     * @param request
     * @param response
     * @param
     * @param addressId
     */
    @RequestMapping(value = "/setDefault", method = {RequestMethod.POST})
    @ApiOperation(value = "设置当前地址为默认地址")
    public void setDefault(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "用户Id") @RequestParam(value = "memberId", required = true) Long memberId,
            @ApiParam(value = "地址id") @RequestParam(value = "addressId", required = true) Long addressId
    ) {
        addressService.setDefaultAddress(addressId, memberId);
        ResponseMessage.success();
    }

    /**
     * 获取默认
     *
     * @param request
     * @param response
     * @param memberId
     */
    @RequestMapping(value = "/getDefault", method = {RequestMethod.POST})
    @ApiOperation(value = "获取默认地址")
    public void getDefaultAddress(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "用户Id") @RequestParam(value = "memberId", required = true) Long memberId
    ) {
        SundayAddress result = addressService.getDefaultAddress(memberId);
        ResponseMessage.success(result);
    }

    /**
     * 获取用户单个地址
     *
     * @param request
     * @param response
     * @param addressId
     */
    @RequestMapping(value = "/findOne", method = {RequestMethod.POST})
    @ApiOperation(value = "获取单个地址")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "地址Id") @RequestParam(value = "addressId", required = true) Long addressId
    ) {
        SundayAddress result = addressService.getById(addressId);
        ResponseMessage.success(result);
    }


    /**
     * 单个删除收货地址
     *
     * @param request
     * @param response
     * @param addressId
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @ApiOperation(value = "删除地址")
    public void delete(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "地址id") @RequestParam(value = "addressId", required = true) Long addressId
    ) {
        addressService.delete(addressId);
        ResponseMessage.success();
    }

}
